package com.hengli.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hengli.constant.MessageConstant;
import com.hengli.constant.StatusConstant;
import com.hengli.dto.WinepackageDTO;
import com.hengli.dto.WinepackagePageQueryDTO;
import com.hengli.entity.Wine;
import com.hengli.entity.Winepackage;
import com.hengli.entity.WinepackageWine;
import com.hengli.exception.DeletionNotAllowedException;
import com.hengli.exception.WinepackageEnableFailedException;
import com.hengli.mapper.WineMapper;
import com.hengli.mapper.WinepackageMapper;
import com.hengli.mapper.WinepackageWineMapper;
import com.hengli.result.PageResult;
import com.hengli.service.WinepackageService;
import com.hengli.vo.WineItemVO;
import com.hengli.vo.WinepackageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author ljx
 * @date 2024/3/23 22:02
 */
@Service
@Slf4j
public class WinepackageServiceImpl implements WinepackageService {

    @Autowired
    private WinepackageMapper winepackageMapper;

    @Autowired
    private WinepackageWineMapper winepackageWineMapper;

    @Autowired
    private WineMapper wineMapper;
    /**
     * 分页查询
     *
     * @param winepackagePageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(WinepackagePageQueryDTO winepackagePageQueryDTO) {
        PageHelper.startPage(winepackagePageQueryDTO.getPage(),winepackagePageQueryDTO.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        Page<WinepackageVO> page = winepackageMapper.pageQuery(winepackagePageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐
     * @param winepackageDTO
     */
    @Override
    @Transactional
    public void saveWithWine(WinepackageDTO winepackageDTO) {
        Winepackage winepackage = new Winepackage();
        BeanUtils.copyProperties(winepackageDTO, winepackage);

        // 向套餐表插入数据
        winepackageMapper.save(winepackage);

        // 获取套餐的主键值
        Long winepackagelId = winepackage.getId();

        // 获取套餐数据
        List<WinepackageWine> winepackageWineList = winepackageDTO.getWinepackageWines();
        if (!winepackageWineList.isEmpty()){
            winepackageWineList.forEach(winepackageWine -> {
                winepackageWine.setWinepackageId(winepackagelId);
            });
            // 向winepackageWine表插入n条数据
            winepackageWineMapper.insertBatch(winepackageWineList);
        }
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        if (ids.isEmpty()){
            return;
        }

        // 判断当前酒品是否能够删除---是否存在起售中的酒品
        for (Long id : ids) {
            Winepackage winepackage = winepackageMapper.getById(id);
            if (winepackage.getStatus() == StatusConstant.ENABLE){
                // 当前套餐为启用状态，不能删除
                throw new DeletionNotAllowedException(MessageConstant.WINEPACKAGE_ON_SALE);
            }
        }

        // 删除套餐表中的数据
        for (Long id : ids) {
            winepackageMapper.deleteById(id);
            // 根据套餐id删除套餐和酒品的关联关系
            winepackageWineMapper.deleteByWinepackageId(id);
        }
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public WinepackageVO getByIdWithWine(Long id) {
        Winepackage winepackage = winepackageMapper.getById(id);
        List<WinepackageWine> winepackageWineList = winepackageWineMapper.getDishByWinepackageId(id);

        WinepackageVO winepackageVO = new WinepackageVO();
        BeanUtils.copyProperties(winepackage, winepackageVO);
        winepackageVO.setWinepackageWines(winepackageWineList);

        return winepackageVO;
    }

    /**
     * 修改套餐
     * @param winepackageDTO
     */
    @Override
    @Transactional
    public void updateWithWine(WinepackageDTO winepackageDTO) {
        Winepackage winepackage = new Winepackage();
        BeanUtils.copyProperties(winepackageDTO, winepackage);

        // 修改套餐基本信息
        winepackageMapper.update(winepackage);

        // 获取套餐id
        Long winepackageId = winepackageDTO.getId();

        // 删除原有的套餐关联的酒品信息，操作winepackage_wine表，执行delete
        winepackageWineMapper.deleteByWinepackageId(winepackageId);

        // 获取套餐关联的酒品信息
        List<WinepackageWine> winepackageWines = winepackageDTO.getWinepackageWines();

        if (!winepackageWines.isEmpty()){
            winepackageWines.forEach(winepackageWine -> {
                winepackageWine.setWinepackageId(winepackageId);
            });
            // 重新向winepackage_wine表插入n条数据
            winepackageWineMapper.insertBatch(winepackageWines);
        }
    }

    /**
     * 修改套餐状态
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        // 判断是起售/停售
        if (status == StatusConstant.ENABLE){
            // 先判断该套餐内的酒品是否包含停售酒品
            List<WinepackageWine> winepackageWineList = winepackageWineMapper.getDishByWinepackageId(id);
            if (!winepackageWineList.isEmpty()) {
                winepackageWineList.forEach(winepackageWine  -> {
                    Long wineId = winepackageWine.getWineId();
                    Wine wine = wineMapper.getById(wineId);
                    if (wine.getStatus() == StatusConstant.DISABLE){
                        // 套餐内有停售酒品
                        throw new WinepackageEnableFailedException(MessageConstant.WINEPACKAGE_ENABLE_FAILED);
                    }
                });
            }
        }

        Winepackage winepackage = Winepackage.builder()
                .id(id)
                .status(status)
                .build();
        winepackageMapper.update(winepackage);
    }

    /**
     * 条件查询
     * @param winepackage
     * @return
     */
    public List<Winepackage> list(Winepackage winepackage) {
        List<Winepackage> list = winepackageMapper.list(winepackage);
        return list;
    }

    /**
     * 根据id查询酒品选项
     * @param id
     * @return
     */
    public List<WineItemVO> getWineItemById(Long id) {
        return winepackageMapper.getWineItemByWinepackageId(id);
    }
}
